﻿using Newtonsoft.Json;
using System;
using System.Windows.Forms;
using xClient.Framework;
using xClient.Framework.StaticResource;

namespace cMES1002
{
    public partial class frmGroupCfgEdit : Form
    {
        private string _strCMD = "ADD";
        private object[] _paramGroup = null;
        public frmGroupCfgEdit(string command, params object[] values)
        {
            InitializeComponent();
            this._strCMD = command;
            this._paramGroup = values;
        }

        private void GetSectionNameList()
        {
            var result = Services.ServiceMethod.GetSectionNameList();
            if (result.Status)
            {
                this.combSectionName.Items.Clear();
                foreach (var tmpSection in result.Data)
                {
                    this.combSectionName.Items.Add(tmpSection.section_name);
                }
            }
        }

        private void InitialFormData()
        {
            this.Text = this._strCMD.Equals("EDIT")? "修改站别信息" : "新增站别信息";
            this.txtGroupCode.Text = "";
            this.txtGroupName.Text = "";
            this.combSectionName.Text = "";
            this.txtGroupDesc.Text = "";
            this.txtRemarks.Text = "";
            this.txtCreateUser.Text = StaticDictionary.Dictionary.UserID;
            this.txtCreateUser.Enabled = false;            
            this.txtUpdateUser.Enabled = false;
            this.dtpCreateTime.Value = DateTime.Now;
            this.dtpCreateTime.Enabled = false;
            this.dtpUpdateTime.Enabled = false;
            this.lblSysAutoCreate.Visible = true;

            if (this._strCMD.Equals("EDIT"))
            {
                this.txtGroupCode.Text = this._paramGroup[0].ToString();
                this.txtGroupName.Text = this._paramGroup[1].ToString();
                this.combSectionName.Text = this._paramGroup[2].ToString();
                this.txtGroupDesc.Text = this._paramGroup[3].ToString(); 
                this.txtRemarks.Text = this._paramGroup[4].ToString();
                this.txtCreateUser.Text = this._paramGroup[6].ToString();
                this.dtpCreateTime.Text = this._paramGroup[7].ToString();
                this.txtUpdateUser.Text = StaticDictionary.Dictionary.UserID;
                this.dtpUpdateTime.Value = DateTime.Now;
                this.lblSysAutoCreate.Visible = false;
            }
  
        }

        private void frmGroupCfgEdit_Load(object sender, EventArgs e)
        {
            //initial section name
            this.GetSectionNameList();
            //initial form data
            this.InitialFormData();
        }

        private bool CheckInputDataValid()
        {
            bool _bResult = true; ;
            if (String.IsNullOrEmpty(this.txtGroupName.Text.Trim()))
            {
                MessageBox.Show("站别必须输入","系统提示");
                return false;
            }
            if (String.IsNullOrEmpty(this.combSectionName.Text.Trim()))
            {
                MessageBox.Show("区域必须输入", "系统提示");
                return false;
            }
            if (String.IsNullOrEmpty(this.combSectionName.Text.Trim()))
            {
                MessageBox.Show("区域必须输入", "系统提示");
                return false;
            }
            return _bResult;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!CheckInputDataValid())
            {
                return;
            }
            var _dataModel = JsonConvert.SerializeObject(new
            {
                group_code = this.txtGroupCode.Text,
                group_name = this.txtGroupName.Text.Trim().ToUpper(),
                section_name = this.combSectionName.Text.Trim().ToUpper(),
                description = this.txtGroupDesc.Text.Trim(),
                remarks =this.txtRemarks.Text.Trim(),
                create_user = this.txtCreateUser.Text,
                create_time =this.dtpCreateTime.Text,
                update_user =this.txtUpdateUser.Text,
                upate_time = this.dtpUpdateTime.Text
            });
            ExecutionResult<dynamic> result = new ExecutionResult<dynamic>();

            if (this._strCMD.Equals("ADD"))
            {
                result = Services.ServiceMethod.AddGroupCfgInfo(_dataModel);
            }
            else if(this._strCMD.Equals("EDIT"))
            {
                result = Services.ServiceMethod.UpdateGroupCfgInfo(_dataModel);
            }
            if (result.Status)
            {
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                this.DialogResult = DialogResult.No;
                MessageBox.Show(result.Message, "系统消息");
            }
        }
    }
}
